package com.xiaoyun.controller;

import com.xiaoyun.bo.ShopcartBO;
import com.xiaoyun.constant.Const;
import com.xiaoyun.pojo.ItemsSpec;
import com.xiaoyun.utils.JsonUtils;
import com.xiaoyun.utils.RedisUtil;
import com.xiaoyun.utils.XYJSONResult;
import com.xiaoyun.vo.ShopcartVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Api("购物车接口")
@RestController
@RequestMapping("cart")
@Slf4j
@RequiredArgsConstructor
public class ShopcartController extends BaseController {

    private final RedisUtil redisUtil;

    @ApiOperation(("添加商品到购物车"))
    @PostMapping("/add")
    public XYJSONResult add(
            @RequestParam String userId,
            @RequestBody ShopcartBO shopcartBO,
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        if (StringUtils.isBlank(userId)) {
            return XYJSONResult.errorMsg("");
        }
        String shoppartJson = redisUtil.getKey(Const.FOODIE_SHOPCART + ":" + userId);
        List<ShopcartBO> shopcartList = null;
        if (StringUtils.isNotBlank(shoppartJson)) {
            shopcartList = JsonUtils.jsonToList(shoppartJson, ShopcartBO.class);
            boolean isHaving = false;
            for (ShopcartBO sc : shopcartList) {
                String tempSecId = sc.getSpecId();
                if (tempSecId.equals(shopcartBO.getSpecId())) {
                    sc.setBuyCounts(sc.getBuyCounts() + shopcartBO.getBuyCounts());
                    isHaving = true;
                }
            }
            if (!isHaving) {
                shopcartList.add(shopcartBO);
            }
        } else {
            shopcartList = new ArrayList<>();
            shopcartList.add(shopcartBO);
        }
        // 同步数据源到 redis缓存
        redisUtil.setKey(Const.FOODIE_SHOPCART + ":" + userId, JsonUtils.objectToJson(shopcartList));

        log.info("userId:{},shopcartBO:{}", userId, shopcartBO);

        return XYJSONResult.ok();
    }

    @ApiOperation("从购物车删除商品")
    @GetMapping("/del")
    public XYJSONResult del(
            @RequestParam String userId,
            @RequestParam String itemSpecId,
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        if (StringUtils.isBlank(itemSpecId) || StringUtils.isBlank(userId)) {
            return XYJSONResult.errorMsg("");
        }

        String shoppartJson = redisUtil.getKey(Const.FOODIE_SHOPCART + ":" + userId);
        if (StringUtils.isNotBlank(shoppartJson)) {
            List<ShopcartBO> shopcartList = JsonUtils.jsonToList(shoppartJson, ShopcartBO.class);
            for (ShopcartBO sc : shopcartList) {
                String tmpSpecId = sc.getSpecId();
                if (tmpSpecId.equals(itemSpecId)) {
                    shopcartList.remove(sc);
                    break;
                }
            }
            // 用户删除购物车数据
            redisUtil.setKey(Const.FOODIE_SHOPCART + ":" + userId, JsonUtils.objectToJson(shopcartList));
        }


        return XYJSONResult.ok();
    }
}
